import { Component, OnInit , ViewChild ,TemplateRef } from '@angular/core';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { NzLayoutModule } from 'ng-zorro-antd/layout';
import { NzButtonModule, NzButtonComponent  } from 'ng-zorro-antd/button';
import { NzFormatEmitEvent  } from 'ng-zorro-antd/core';
import { NzMessageService, NzModalRef, NzModalService } from 'ng-zorro-antd';
import { FormBuilder, FormControl, FormGroup  } from '@angular/forms';
import { DelonFormModule,SFSchema , SFComponent , WidgetRegistry, SFSchemaEnumType, SFTextareaWidgetSchema } from '@delon/form';
import { HttpService, SupDic, GridComponent } from 'lbf';
import { Subject } from 'rxjs';
import { STColumn, STColumnTag, STColumnBadge } from '@delon/abc';

@Component({
  selector: 'app-dzda-sxpzwh',
  templateUrl: './sxpzwh.component.html',
  styleUrls: ["./sxpzwh.component.less"]
})
export class DzdaSxpzwhComponent implements OnInit {
  searchValue = ''
  nodes = [];
  isAddDisabled = true;
  treeData;
  isVisible = false;
  isPzVisible = false;
  isUpVisible = false;
  modalTitle = '新增凭证';
  modalPzTitle = '凭证列表';
  param = {sname: 'dzda_pzmucx',queryparas: {}}
  upData;
  style = {height: (window.outerHeight-330)+'px',overflow: 'auto'}

  showSchema: SFSchema = {
    properties:{
      DAD017:{
        type: 'string',
        title: '事项主键',
        ui:
        {
          hidden: true
        }
        
      },
      DAD004:{
        type: 'string',
        title: '权力编码',
        ui:{
          widget:'text',
          grid:{
            span:8
          }
        }
      },
      DAD031:{
        type: 'string',
        title: '事项名称',
        ui:{
          widget:'text',
          grid:{
            span:16
          }
        }
      },
      DAD032: {
        type: 'string',
        title: '对象主体',
        enum: this.supdic.getSFDic('DAD032'),
        readOnly: true,
        ui: {
          widget: 'radio',
          grid:{
            span:8
          }
        }
      },      
      
      DGB010: {
        type: 'string',
        title: '所属部门',
        ui:{
          widget:'text',
          grid:{
            span:8
          }
        }
      },
      DAD011: {
        type: 'string',
        title: '事项类型',
        ui:{
          widget:'text',
          grid:{
            span:8
          }
        }
      },
      
    },
    ui:{
      spanLabelFixed: 90,
      grid:{
        span:24
      }
    }
  };

  addSchema: SFSchema = {
    properties: {
      DAD017: {
        type: 'string',
        title: '事项编码',
        enum: this.supdic.getSFDic('DAD017'),
        ui: {
          widget: 'select'
        },
        readOnly: true,
      },
      custom: {
        type: 'string',
        title: '凭证名称',
        ui: {
          widget: 'custom'
        }
      },
      DAH001: {
        type: 'number',
        title: '凭证ID',
        ui: {
          hidden: true
        }
      },
      AAE013: {
        type: 'string',
        title: '备注',
        ui: {
          widget: 'textarea',
          autosize: { minRows: 1, maxRows: 6 },
        } as SFTextareaWidgetSchema,
      },
      SEQ: {
        type: 'number',
        title: '顺序'
      }
    },
    required: ['custom']
  }

  upSchema: SFSchema = {
    properties: {
      DAD017: {
        type: 'number',
        title: '事项编码',
        enum: this.supdic.getSFDic('DAD017'),
        ui: {
          widget: 'select'
        },
        readOnly: true,
      },
      DAH001: {
        type: 'number',
        title: '凭证编码',
        enum: this.supdic.getSFDic('DAH001'),
        ui: {
          widget: 'select'
        },
        readOnly: true
      },
      SEQ: {
        type: 'number',
        title: '顺序'
      },
      AAE013: {
        type: 'string',
        title: '备注',
        ui: {
          widget: 'textarea',
          autosize: { minRows: 1, maxRows: 6 },
        } as SFTextareaWidgetSchema,
      },
    },
    required: ['AAE013']
  }

  searchSchema: SFSchema = {
    properties: {
      DAH002: {
        title: '凭证名称',
        type: 'string'
      },
      DAD130: {
        type: 'string',
        title: '凭证标签',
        enum: this.supdic.getSFDic('PZBQ'),
        ui: {
          widget: 'select',
          mode: 'tags',
          width: 400,
          allowClear: true
        }
      }
    }
  }

  columns: STColumn[] = [
    { title: '凭证编码',index: 'DAH001'},
    { title: '凭证名称',index: 'DAH002',format: (record)=> this.supdic.getdicLabel('DAH001',record.DAH001)},
    { title: '备注', index: 'AAE013'},
    { title: '顺序',index: 'SEQ'},
    {
      title: '操作区',
      buttons: [
        {
          text: '修改',
          click: (record) => this.update(record)
        },
        {
          text: '删除',
          click: (record) => this.del(record),
          pop: true,
          popTitle: '是否删除？'
        }
      ]
    }
  ]

  pzColumns: STColumn[] = [
    { title: '凭证编码', index: 'DAH001'},// ,dic: 'DAH001'},
    { title: '凭证名称', index: 'DAH002' },
    { title: '凭证类别',index: 'DAH003',dic: 'DAH003'},
    { title: '是否电子化',index: 'DAH004',dic: 'DAH004'},
    { title: '是否数据共享',index: 'DAH005',dic:'DAH005'},
    { title: '所属部门',index: 'DGB010',dic: 'DGB010'},
    {
      title: '操作区',
      buttons: [
          {
            text: '添加',
            click: (record) => this.tjpz(record)
          }
      ]
    }
  ]

  @ViewChild('showSf',{static:false}) showSf: SFComponent;
  @ViewChild('addSf',{static:false}) addSf: SFComponent;
  @ViewChild('upSf',{static:false}) upSf: SFComponent;
  @ViewChild('st',{static: false}) st: GridComponent;
  @ViewChild('pzst',{static: false}) pzst: GridComponent;
  @ViewChild('sf',{static:false}) sf: SFComponent;
  constructor(private httpservice: HttpService,
    private supdic: SupDic,
    private msgSrv: NzMessageService) { }

  ngOnInit() { 
    this.initDeptTree();
  }

  // 初始化机构树
  initDeptTree(){
    this.httpservice.lbservice('BDWH_TREE',{}).then(res=>{
      console.log(res.message.list);
      this.nodes = res.message.list;
    });
 }

  // 功能树单击事件
  nzEvent(event: NzFormatEmitEvent): void {
    this.isAddDisabled = true
    this.showSf.refreshSchema();
    this.treeData = event.node.origin;
    // console.log(this.addSf);
    // console.log(event);
    if(this.treeData.isLeaf)
    {
        // 编辑框表单赋值
        this.showSf.setValue('/DAD017',this.treeData.key);
        this.showSf.setValue('/DAD031',this.treeData.title);
        this.showSf.setValue('/DAD004',this.treeData.DAD004);
        this.showSf.setValue('/DGB010',this.treeData.DGB010);
        this.showSf.setValue('/DAD032',this.treeData.DAD032);
        this.showSf.setValue('/DAD011',this.treeData.DAD011);

        this.isAddDisabled = false;
        // console.log(this.sf.getValue('/SEQ'));
        // 如果是一件事，则 子项表单提取 按钮 显示
        // if(this.treeData.SXLB==='1')
        // {
        //   this.isAddDisabled = true;
        // }
        // else
        // {
        //   this.isAddDisabled = false;
        // }
        // this.isAddDisabled = false;
        this.st.reload({DAD017: this.treeData.key})
    }
    
  }

  add(){
    this.addSf.refreshSchema();
    this.addSf.setValue('/DAD017',this.treeData.key);
   
    this.isVisible = true
  }

  tjpz(rec){
      this.addSf.setValue('/custom',rec.DAH002);
      this.addSf.setValue('/DAH001',rec.DAH001);
      this.isPzVisible = false;
  }

  pzAdd(){
    this.addSf.setValue('/custom',null);
    this.sf.refreshSchema();
    this.pzst.reload({});
    this.isPzVisible = true
  }

  handleAddCancel(){
    this.isVisible = false
  }

  handleAddOk(){
    this.isVisible = false
    this.httpservice.lbservice('SXPZWH_PZXZ',{para: this.addSf.value}).then(resdata => {
      if(resdata.code<1)
      {
         this.msgSrv.error(resdata.errmsg);
      }
      else
      {
        this.msgSrv.success('添加成功');
        this.st.st.reload();
      }
    })
    // this.st.reload({para: {DAD017: this.treeData.key}});
    
  }

  handlePzCancel(){
    this.isPzVisible = false;
  }

  handleUpCancel(){
    this.isUpVisible = false;
  }

  saveUp(){
    this.isUpVisible = false;
    this.httpservice.lbservice('SXPZWH_PZXG',{para: this.upSf.value}).then(resdata => {
      if(resdata.code<1)
      {
         this.msgSrv.error(resdata.errmsg);
      }
      else
      {
        this.msgSrv.success('修改成功');
        this.st.st.reload();
      }
    })
  }

  update(rec){
    this.upData = rec;
    this.isUpVisible = true;
    
  }

  del(rec){
    this.httpservice.lbservice('SXPZWH_PZSC',{DAD017: rec.DAD017,DAH001: rec.DAH001}).then(resdata => {
      if(resdata.code<1)
      {
         this.msgSrv.error(resdata.errmsg);
      }
      else
      {
        this.msgSrv.success('删除成功');
        this.st.st.reload();
      }
    })
  }

  query(){
    this.pzst.reload(this.sf.value);
  }
}
